import 'package:aihu_getx_app/app/modules/d/d_home/controllers/d_home_controller.dart';
import 'package:aihu_getx_app/app/modules/d/d_my/models/user_job_card_resp_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_my/providers/d_my_provider.dart';
import 'package:aihu_getx_app/utils/share_poster_utils.dart';
import 'package:carousel_slider/carousel_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DMyUserJobCardController extends DHomeController {
  final String title = "我的工卡";

  final count = 0.obs;
  final current = 0.obs;
  final CarouselSliderController carouselController = CarouselSliderController();
  final repaintWidgetKey = GlobalKey(); // 绘图key值

  final userJobCardList = <UserJobCardRespVO>[].obs;
  final loading = false.obs;

  void reset({int? offset}) {
    if (null != offset) {
      offset > 0
          ? carouselController.nextPage()
          : carouselController.previousPage();
    }
  }

  void changeQrBg() {
    userJobCardList[current.value].changeQrBg();
    // if (userJobCardList.isNotEmpty) {
    //   userJobCardList[current.value].qrBgIndex.value =
    //       (userJobCardList[current.value].qrBgIndex.value + 1) %
    //           userJobCardList[current.value].qrBg!.length;
    //   update();
    // }
  }

  void savePhoto() {
    SharePosterUtils.savePosterImage(repaintWidgetKey);
  }

  void share() {
    SharePosterUtils.onSharePlusShare(repaintWidgetKey);
  }

  Future onRefresh() {
    if (loading.isTrue) {
      return Future.value();
    }
    onLoading();
    return Future.value();
  }

  void onLoading() async {
    loading.value = true;
    try {
      var res = await DMyProvider.getMyUserJobCard();
      userJobCardList.value = res;
    } finally {
      loading.value = false;
    }
    update();
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    onLoading();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;
}
